import {
  C0LUMNS,
  INVOICE_COLUMNS,
  OTHER_COLUMNS,
} from './index.const';
import {
  formatDate
}
from '@const/filter.js';
import {
  queryOrderInfo,
  orderGetAppendInfo,
  distributorProductList,
  invoiceInfoList,
  otherAttachmentList
} from '@api/m_mall_web_v1.js';
import TableUpload from '~/modules/table-upload';
import ElEditable from '@components/el-editable';
export default {
  components: {
    ElEditable,
    TableUpload
  },
  data() {
    return {
      currentPage: 1,
      pageSize: 20,
      tableLoading: false,
      payload: {},
      C0LUMNS,
      INVOICE_COLUMNS,
      OTHER_COLUMNS,
      detailData: {},
      supplierVo: {},
      addressResDto: {},
      orderAppendPOS: [],
      contractVo: {},
      activeName: 'first',
      otherData: [],
      invoiceData: [],
    };
  },
  async created() {
    await this.getDetail()

  },
  computed: {
    orderId() {
      return this.$route.query.id;
    }
  },
  methods: {
    //获取详情
    async getDetail() {
      await this.$axios.$post(queryOrderInfo, {
        orderId: this.orderId
      }).then(res => {
        if (res.code === '0' && res.payload) {
          this.supplierVo = res.payload.supplierVo
          this.addressResDto = res.payload.addressResDto
          this.orderAppendPOS = res.payload.orderAppendPOS
          this.contractVo = res.payload.contractVo
          this.detailData = {
            ...res.payload
          }

          this.detailData.orderAmount = this.detailData.orderAmount !== null ? this.detailData.orderAmount.toFixed(2) : '0.00'
          this.detailData.saleDate = this.detailData.saleDate ? formatDate(this.detailData.saleDate, 'YYYY-MM-DD') : ""
          this.getAppendInfo(res.payload.contractVo)
          this.getList()
        }
      })
    },
    getAppendInfo(obj) {
      this.$axios.$post(orderGetAppendInfo, {
        productLineCode: obj.productLineCode,
        vendorNum: obj.supplierCode,
        ddbm: 'WHOLESALE_ISSUE',
        busModel: 4,
        page: 1,
        pageSize: 9999,
      }).then(res => {
        if (res.code === '0' && res.payload) {
          this.orderAppendPOS.forEach(item => {
            if (item.attributeType === 3) {
              item.attributeValue = item.selectName
            }
            // else {
            //   res.payload.forEach(it => {
            //     if (item.attributeName === it.attributeName) {
            //       let list = it.valueRange ? JSON.parse(it.valueRange) : []
            //       list.forEach(val => {
            //         if (val.value === item.attributeValue) {
            //           item.attributeValue = val.label
            //         }
            //       })
            //     }
            //   })
            // }
          })
        }
      })
    },
    //获取发票信息
    getPayment() {
      this.$axios.$post(invoiceInfoList, {
        newspaperCode: this.detailData.orderCode,
        page: 1,
        pageSize: 9999,
      }).then(res => {
        if (res.payload) {
          this.invoiceData = res.payload
        }
      })
    },
    //获取其他附件
    getLogistics() {
      this.$axios.$post(otherAttachmentList, {
        newspaperCode: this.detailData.orderCode,
        page: 1,
        pageSize: 9999,
      }).then(res => {
        if (res.payload) {
          this.otherData = res.payload
        }
      })

    },
    //底部tab切换调用不同接口
    handleClick(tab, event) {
      if (tab.name === 'second') {
        this.getPayment()
      }
      if (tab.name === 'third') {
        this.getLogistics()
      }
    },
    //获取详情商品列表
    getList() {
      this.tableLoading = true
      this.$axios.$post(distributorProductList, {
        newspaperCode: this.detailData.orderCode,
        page: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        this.payload = res.payload;
        this.payload.content.map(item => {
          item.productCount = parseFloat(item.productCount)
        })
        this.tableLoading = false
      })
    },
    reloadPagination(val) {
      this.currentPage = val.number
      this.pageSize = val.size
      this.getList()
    }
  }
};
